<?php
//url相关
class lib_url{
	
	public static function HTTP_REFERER(){
		$session = Session::instance();
		
		if (isset($_SERVER['REDIRECT_URL']))
			$session->set('url',"{$_SERVER['REDIRECT_URL']}");
		if (isset ( $_SERVER ['HTTP_REFERER'] ) && isset ( $_SERVER ['REDIRECT_URL'] )) {
			$HTTP_REFERER = parse_url ( $_SERVER ['HTTP_REFERER'] );
			$paramId = Kohana_Request::$current->param ( 'id' );
			if (! $session->get ( 'ref' ) || trim ( $HTTP_REFERER ['path'], $paramId . '/' ) != trim ( $_SERVER ['REDIRECT_URL'], $paramId . '/' )) {
				$session->set('ref',"{$_SERVER['HTTP_REFERER']}");
			}
		} else {
			$session->set('ref',lib_url::base($session->get('url')));
		}
		
		return $session->get('ref');
	}
	
	//url重定向
	public static function redirect($url = '/'){
		Kohana::$base_url = Kohana::$config->load('common')->host;
		Request::current()->redirect($url);
	}
	
	public static function base($uri = ''){
		return project::config()->host.trim((string)$uri,'/');
	}
	
	public static function get_uri(){
		if (is_object(Request::current())){
			return Request::current()->uri();
		}else{
			return $_SERVER['REQUEST_URI'];
		}
	}
	
	public static function get_query(){
		return URL::query();
	}
	
	public static function get_url(){
		return self::base().self::get_uri().self::get_query();
	}
}